<?php
require_once(APPPATH.'libraries/paginationobject.php');
require_once(APPPATH.'views/components/page.php');

class Category extends CI_Controller
{
	
	function Category()
	{
		parent::__construct();
		
		// Loading models
		$this->load->model('category_model', 'category');
		$this->load->model('product_model', 'product');
		$this->load->model('producttype_model', 'productType');
	}
	
	public function index()
	{
		if (Page::getInstance()->isAdmin())
		{
			$data['categories'] = $this->category->getAllCategories();
			$this->load->view('categories/category_admin_view', $data);
		}else{
			$error = ERROR_NONE;		
			$this->load->view('category_view');
		}
	}
	
	public function getAllCategories()
	{
		return $this->category->getAllCategories();
	}
	
	public function insert()
	{
		$error = ERROR_NONE;
		
		if (Page::getInstance()->isAdmin())
		{
			$name = $this->input->post('categoryname');
			$desc = $this->input->post('description');
			$image = $this->input->post('image');
			
			if ($this->category->findCategory($name) < 0)
				$this->category->insertCategory($name, $desc, $image);
			else
			{
				$error = EXISTED_ERROR;
				$this->session->set_userdata('categoryname', $name);
			}
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('error', $error);
		redirect('category');
	}
	
	public function sortview()
	{
		$error = ERROR_NONE;
		
		if (Page::getInstance()->isAdmin())
		{
			$error = $this->session->set_userdata('sort', TRUE);
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('error', $error);
		redirect('category');
	}
	
	public function saveSorting()
	{
		$error = ERROR_NONE;
		
		if (Page::getInstance()->isAdmin())
		{
			$ids = $this->input->post('ids');
			$orders = $this->input->post('orders');
			
			$error = $this->category->updateOrder($ids, $orders);
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('error', $error);
		redirect('category');
	}
	
	public function delete()
	{
	   $error = ERROR_NONE;
	   
	   if (Page::getInstance()->isAdmin())
	   {
	   		$ids = $this->input->post('categories');
	   		$error = $this->category->deleteMultipleCategories($ids);
	   }
		else
		{
			redirect(site_url());
		}
	   
	   $this->session->set_userdata('error', $error);
        redirect('category');
	}

	public function edit($id)
	{
		if (Page::getInstance()->isAdmin())
		{
			$category = $this->category->getCategoryById($id);
			
			if ($category->getId() <= 0)
				$data['rerror'] = NOT_EXISTED_ERROR;
			
			$data['category'] = $category;
			$this->load->view('categories/category_admin_detail', $data);
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function view($cateId)
	{
		$error = ERROR_NONE;
		
		$this->session->set_userdata('categoryid', $cateId);
						
		$producttypes = $this->productType->getProductTypesByCategoryId($cateId);
		$category = $this->category->getCategoryById($cateId);
		$data['producttypes'] = $producttypes;
		$data['category'] = $category;
		
		if ($category->getId() <= 0)
			$data['rerror'] = NOT_EXISTED_ERROR;
		
		$this->load->view('category_producttype_view', $data);
	}

	public function update()
	{
		$error = ERROR_NONE;
		$id = $this->input->post('categoryid');
		$name = $this->input->post('categoryname');
		$name = quotes_to_entities($name);
		$desc = $this->input->post('description');
		$image = $this->input->post('image');
		
		if (Page::getInstance()->isAdmin())
		{
			$category = $this->category->getCategoryById($id);
			
			$foundId = $this->category->findCategory($name);
			$existed = FALSE;
			
			if ($foundId == $id)
				$existed = FALSE;
			else {
				if ($foundId > 0)
					$existed = TRUE;
			}
		
			if (! $existed)
			{
				$category->setTitle($name);
				$category->setDescription($desc);
				$category->setImage($image);
				$error = $this->category->updateCategory($id, $category);
			}
			else
			{
				$this->session->set_userdata('categoryname', $name);
				$error = EXISTED_ERROR;
			}
		}
		else
		{
			redirect(site_url());
		}
		
		$this->session->set_userdata('error', $error);
		
		if ($error == ERROR_NONE)
			redirect('category');
		else
		{
			$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : site_url();
			redirect($url);
		}
	}
}
/* End of file category.php */
/* Location: ./application/controllers/category.php */